package ltd.followkba.page;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;

import ltd.advskin.MRAM;
import ltd.advskin.MSKIN;
import ltd.advskin.P;
import ltd.advskin.VARIA;
import ltd.advskin.base.BasePage;
import ltd.advskin.base.KcGridLayoutManager;
import ltd.advskin.base.KcListHolder;
import ltd.advskin.base.KcListListener;
import ltd.advskin.base.KcTypeListener;
import ltd.advskin.utils.AdvBitmap;
import ltd.advskin.utils.SysBitmap;
import ltd.advskin.view.KcBottomView;
import ltd.advskin.view.KcHeaderView;
import ltd.advskin.view.KcListView;
import ltd.followkba.MAPP;
import ltd.followkba.MainActivity;
import ltd.followkba.R;

import static android.view.View.VISIBLE;
import static ltd.advskin.MSKIN.MSTRING;
import static ltd.advskin.utils.AdvBitmap.C3_FG_NORMAL;
import static ltd.advskin.view.KcListView.LIST_SPAN_SIZE;

public class ThemeStylePage extends BasePage {
    private String[] gsTextName = MSKIN.getStringArray(R.array.color_type);
    private int[] titleText;
    private int[] titleLineBg;
    private int[] titleMainBg;
    private int[] boxFillingBg;
    private int[] mainTextColor;
    private int[] mainBackground;
    private int[] bottomBackground;
    private RelativeLayout clNormalList;
    private byte gLastThemeStyle;
    private Bitmap[] mBitmap;

    @Override
    public void onInitView(){
        MSTRING("ThemeStylePage onInitView A");
        setLayout(R.layout.normal_list_activity);

        mKcHeaderView = (KcHeaderView) findViewById(R.id.khvNormalList);
        mKcHeaderView.setTitle(MSKIN.getString(R.string.skin_setting), new KcTypeListener() {
            @Override
            public void onMessage(int type) {
                switch (type){
                    case KcHeaderView.TYPE_CLICK_LEFT:
                        MRAM.gThemeStyle = gLastThemeStyle;
                        MRAM.g4ThemeStyle = VARIA.tabThemeColor[MRAM.gThemeStyle];
                        setClose();
                        break;
                }
            }
        });
        mKcBottomView = (KcBottomView) findViewById(R.id.kguNormalList);
        mKcBottomView.initView(new KcTypeListener() {
            @Override
            public void onMessage(int type){
                switch (type){
                    case 1:
                        setNext();
                        break;
                }
            }
        });
        mKcBottomView.setItem(1, -1, MSKIN.getString(R.string.sure));
        mKcBottomView.setVisibility(VISIBLE);

        clNormalList = (RelativeLayout) findViewById(R.id.rlNormalList);

        initListView(findViewById(R.id.klvNormalList));
        MSTRING("ThemeStylePage onCreate A ");
        int length = VARIA.tabThemeColor.length;
        titleText = new int[length];
        titleLineBg = new int[length];
        titleMainBg = new int[length];
        boxFillingBg = new int[length];
        mainTextColor = new int[length];
        mainBackground = new int[length];
        bottomBackground = new int[length];
        gLastThemeStyle = MRAM.gThemeStyle;
        for (int counter = 0; counter < length; counter++) {
            getActivity().setTheme(VARIA.tabThemeColor[counter]);
            titleText[counter] = MSKIN.getThemeColor(R.attr.titleText);
            titleLineBg[counter] = MSKIN.getThemeColor(R.attr.titleLineBg);
            titleMainBg[counter] = MSKIN.getThemeColor(R.attr.titleMainBg);
            boxFillingBg[counter] = MSKIN.getThemeColor(R.attr.boxFillingBg);
            mainTextColor[counter] = MSKIN.getThemeColor(R.attr.mainTextColor);
            mainBackground[counter] = MSKIN.getThemeColor(R.attr.mainBackground);
            bottomBackground[counter] = MSKIN.getThemeColor(R.attr.bottomBackground);
        }
        getActivity().setTheme(MRAM.g4ThemeStyle);
        initColor();
    }

    @Override
    public void onInitData(){                                                                       // onInitView结束，开始加载数据
        setSize(VARIA.tabThemeColor.length);
    }
    @Override
    public void onPageUpdate(BasePage basePage){    // 更新显示页面数据，用于刷新显示 basePage返回上面的页面，可以使用getPageResult()获得返回的内容
    }
    private void setNext(){
        MAPP.isChangThemeStyle = true;
        getActivity().startActivity(new Intent(getActivity(), MainActivity.class));
        getActivity().finish();
    }

    private void initColor(){
        MSTRING(String.format("ThemeStylePage initColor C %s", MRAM.gThemeStyle));

        AdvBitmap.resetLruCache();
        clNormalList.setBackgroundColor(mainBackground[MRAM.gThemeStyle]);
        int length = mKcHeaderView.mBText.length;
        for (int counter = 0; counter < length; counter++){
            mKcHeaderView.setTextColor(counter, titleText[MRAM.gThemeStyle], C3_FG_NORMAL);     // 标题文字颜色
        }
        mKcHeaderView.setBottomLineColor(titleLineBg[MRAM.gThemeStyle]);
        mKcHeaderView.setBackgroundColor(titleMainBg[MRAM.gThemeStyle]);

        mKcBottomView.mKcBtnUnion[1].setTextColor(titleText[MRAM.gThemeStyle], C3_FG_NORMAL);
        mKcBottomView.setTopLineColor(titleLineBg[MRAM.gThemeStyle]);
        mKcBottomView.setBackgroundColor(bottomBackground[MRAM.gThemeStyle]);
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // KcListView的全部处理，放在页面的最后位置统一处理 ///////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    private final int[] item_theme_style_view = new int[]{
            R.layout.item_theme_style_view, 4,
            R.id.tvNameThemeStyle, R.id.ivBGThemeStyle, R.id.ivPadThemeStyle, R.id.ivChoiceThemeStyle,
    };
    private KcListView mKcListView;
    private void setListView(){
        if (mKcListView != null) {
            mKcListView.setData(true);
        }
    }
    private void setSize(int length){
        if (mKcListView != null) {
            mKcListView.setSize(length);
        }
    }
    private void initListView(final View view){
        mKcListView = (KcListView)view;
        KcGridLayoutManager layoutManager = new KcGridLayoutManager(getActivity(), 2);
        mKcListView.initView(null, item_theme_style_view, layoutManager, new KcListListener() {
            @Override
            public int onMessage(int type, int tab, Object object){
                switch (type) {
                    case LIST_SPAN_SIZE:              // 每行个数不规则的调整(需要这个layoutManager)  spanCount（数值随便定）除以return（数值随便定)后面的数字，就是等于你一行需要的个数
                        return 1;
                }
                return 0;
            }

            @Override
            public int onBindItemHolder(boolean skipImage, int tab, int position, final KcListHolder holder) {
                MSKIN.setBitmap(P.theme_style_true,holder.mView[3]);
                if ((position) == MRAM.gThemeStyle){   // 选中当前主题时
                    holder.mView[3].setVisibility(View.VISIBLE);
                    MSKIN.setBitmap(0, P.theme_style_true, holder.mView[3]);
                }else {
                    holder.mView[3].setVisibility(View.GONE);
                }
                String gsLocal_2;  // 主题类型文字
                gsLocal_2 = gsTextName[(position) % gsTextName.length];
                MSKIN.setText(gsLocal_2, holder.mView[0]);
                if (mBitmap == null){
                    mBitmap = new Bitmap[VARIA.tabThemeColor.length * 2];
                    final View kcBtnImage0 = holder.mView[1];
                    final View kcBtnImage1 = holder.mView[2];
                    ViewTreeObserver vto = kcBtnImage0.getViewTreeObserver();
                    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            kcBtnImage0.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//MSTRING(String.format("PageImage checkImageView C %s %s %s", bBitmap.FileName, bBitmap.mImageView.getWidth(), bBitmap.mImageView.getHeight()));
                            int size0 = kcBtnImage0.getWidth();
                            int size1 = kcBtnImage1.getWidth();
                            SysBitmap sysBitmap = new SysBitmap();
                            int g4Local_1;
                            for (int counter = 0; counter < VARIA.tabThemeColor.length; counter++){
                                g4Local_1 = counter * 2;
                                mBitmap[g4Local_1] = sysBitmap.makeColorBitmap(AdvBitmap.C3_BGC_TYPE3, boxFillingBg[counter], 128, 128, size0, size0);
                                mBitmap[g4Local_1+1] = sysBitmap.makeColorBitmap(AdvBitmap.C3_BGC_TYPE1, mainBackground[counter], 128, 128, size1, size1);
                            }
                            setListView();
                        }
                    });
                    return 0;
                }
                int g4Local_1 = position * 2;
                AdvBitmap.setImageBitmap(mBitmap[g4Local_1],holder.mView[1]);
                AdvBitmap.setImageBitmap(mBitmap[g4Local_1+1],holder.mView[2]);
//((KcLayoutScale)holder.mItemView).setAspectRatio((float) 9 / (float)16);
                holder.mItemView.setTag(position);
                holder.mItemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = (int) v.getTag();
                        getActivity().setTheme(MRAM.g4ThemeStyle);
                        MSKIN.setTheme(position);
                        initColor();
                        setListView();
                    }
                });
                return 0;
            }
        });
    }
}
